package com.gimi.cloud.jimi.dao.dao.prod;

import com.gimi.cloud.jimi.dao.common.SqlProvider;
import com.gimi.cloud.jimi.dao.dao.sys.BaseDao;
import com.gimi.cloud.jimi.dao.myMapper.MyMapper;
import com.gimi.cloud.jimi.dto.prod.ProdCatModelDTO;
import com.gimi.cloud.jimi.entity.prod.ProdCatModelEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @auther loongs
 * @date 2017/12/18  15:52
 */
@Mapper
public interface ProdCatModelDao extends MyMapper<ProdCatModelEntity>,BaseDao<ProdCatModelEntity,ProdCatModelDTO> {

    @SelectProvider(method = "sql",type = SqlProvider.class)
    List<ProdCatModelDTO> queryPage(String sql);

    /**
     * 品类模板后台页面展示，支持条件查询
     * @param prodCatModelDTO
     * @return
     */
    List<ProdCatModelDTO> queryPageList(ProdCatModelDTO prodCatModelDTO);

    /**
     * 插入一条，且返回ID
     * @param prodCatModelDTO
     * @return
     */
    @Options( useGeneratedKeys = true,keyProperty = "modelId",keyColumn = "model_id")
    @Insert("INSERT INTO ec_prod_cat_model ( `model_id`,`prod_cat_id`,`prod_prop_name`,`seq`,`is_del`,`add_uid`,`add_uname`,`add_time`,`update_uid`,`update_uname`,`update_time`) VALUES " +
            "( #{modelId},#{prodCatId},#{prodPropName},#{seq},#{isDel},#{addUid},#{addUname},#{addTime},#{updateUid},#{updateUname},#{updateTime})")
    int insertReturnKey(ProdCatModelDTO prodCatModelDTO);


    /**
     * 删除模板，逻辑删除/启用
     * @param prodCatModelDTO
     */
    @Update("UPDATE ec_prod_cat_model SET is_del = #{isDel} where model_id=#{modelId}")
    Integer deleteModel(ProdCatModelDTO prodCatModelDTO);
    /**
     * 根据品类ID获取模板属性值
     * @param prodCatId
     * @return
     */
    List<ProdCatModelDTO> getCatModelByCatId(String prodCatId);

    @Select("select * from ec_prod_cat_model epcm where epcm.prod_cat_id in " +
            "(select DISTINCT  ep.prod_cat_id from ec_prod_status eps LEFT JOIN ec_product ep on eps.prod_id =ep.prod_id where prod_status =1) " +
            "and epcm.model_id=#{modelId}")
    ProdCatModelEntity selectProductIsPublished(@Param("modelId") Long modelId);


    @Select("select epcm.* from ec_prod_cat_model epcm LEFT JOIN ec_prod_cat_model_value epcmv on epcm.model_id=epcmv.model_id where epcmv.cm_id=#{cmId}")
    ProdCatModelEntity selectModelByCmId(@Param("cmId") Long cmId);
    
    
    @Select("select * from ec_prod_cat_model where prod_cat_id=#{prodCatId} AND is_del = 1")
    List<ProdCatModelEntity> findCatModelByCatId(@Param(value = "prodCatId") String prodCatId);

    /**
     * 通过prodCatId 与prodPropName查询不在指定ModelId中的总数
     * @param prodCatId 品类id
     * @param prodPropName 品类属性名称
     * @param modelId 品类属性id
     * @return 查询总数
     * @throws Exception
     */
    @Select("SELECT count(*) FROM ec_prod_cat_model WHERE is_del = 1 AND prod_cat_id =#{prodCatId}  AND prod_prop_name = #{prodPropName} AND model_id != IFNULL(#{modelId},-1)")
    int selectCountByCatIdAndPropNameExcludeModelId(@Param("prodCatId")String prodCatId,@Param("prodPropName") String prodPropName,@Param("modelId")Long modelId);

    /**
     * 根据品类id获取模版属性集合
     * @param prodCatId 品类id
     * @return 模版属性集合
     * @throws Exception
     */
    @Select("SELECT * FROM ec_prod_cat_model WHERE is_del = 1 and prod_cat_id = #{prodCatId}")
    List<ProdCatModelDTO> queryEcprodCatModelByCatId(@Param("prodCatId") String prodCatId) ;
}
